package com.software.usercenter.service;

import com.software.usercenter.entity.StudentInfo;
import com.software.usercenter.repository.StudentInfoRepository;
import com.software.usercenter.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Transactional
public class StudentInfoService {
    @Autowired
    private StudentInfoRepository studentInfoRepository;

    //新增学生
    public StudentInfo addStudent(StudentInfo studentInfo) {
        String md5data = SecurityUtil.md5String(studentInfo.getPassword());
        String md5Password=SecurityUtil.upperCaseMd5(md5data);
        studentInfo.setPassword(md5Password);
        Date date = new Date();
        /**
         * 注意这里只是为了不改动数据库，将class_id征用为账号创建时间
         */
        studentInfo.setClassId(date.getTime()+"");
        StudentInfo save = studentInfoRepository.save(studentInfo);
        return save;
    }

    //根据id查询学生
    public StudentInfo findStudentInfoByUserId(String userId) {
        StudentInfo find = studentInfoRepository.findStudentInfoByUserId(userId);
        return find;
    }

    //判断用户名是否存在
    public List<StudentInfo> findStudentInfosByUserName(String param_username) {
        return studentInfoRepository.findStudentInfosByUserName(param_username);
    }

    //验证用户名密码是否正确
    public List<StudentInfo> verifyLoginUser(String userName, String password) {
        String md5data=SecurityUtil.md5String(password);
        String md5Password=SecurityUtil.upperCaseMd5(md5data);
        List<StudentInfo> studentInfo=studentInfoRepository.findStudentByUserNameAndPassword(userName,md5Password);
        return studentInfo;
    }

    public Page<StudentInfo> findAll(Integer pageNo, Integer pageSize) {
        Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.by(Sort.Direction.DESC, "classId"));
        return studentInfoRepository.findAll(pageable);
    }

    //更新学生信息
    public void updateStudent(StudentInfo studentInfo) {
        String userName=studentInfo.getUserName();
        String userId= studentInfo.getUserId();
        String nickName=studentInfo.getNickName();
        String realName=studentInfo.getRealName();
        String gender=studentInfo.getGender();
        String school=studentInfo.getSchool();
        String icon=studentInfo.getIcon();
        studentInfoRepository.updateStudent(userId,userName,nickName,realName,gender,school,icon);
    }

    //查找某班级里的所有学生
    public List<StudentInfo> findStudentsByClassId(String classId) {
        return studentInfoRepository.findStudentsByClassId(classId);
    }
    //修改密码界面验证密码
    public List<StudentInfo> confirmPassword(StudentInfo studentInfo) {
        String password=studentInfo.getPassword();
        String md5data=SecurityUtil.md5String(password);
        String md5Password=SecurityUtil.upperCaseMd5(md5data);
        String userName=studentInfo.getUserName();
        List<StudentInfo> list=studentInfoRepository.findStudentByUserNameAndPassword(userName,md5Password);
        return list;
    }

    public void updatePassword(StudentInfo studentInfo) {
        String password=studentInfo.getPassword();
        String md5data=SecurityUtil.md5String(password);
        String md5Password=SecurityUtil.upperCaseMd5(md5data);
        String userName=studentInfo.getUserName();
        studentInfoRepository.updatePassword(userName,md5Password);

    }

    public void deleteStudent(StudentInfo studentInfo) {
        String userName=studentInfo.getUserName();
        studentInfoRepository.deleteStudent(userName);

    }

    //动态查询
    public Page<StudentInfo> findStudentInfosByUserNameLikeAndNickNameLikeAndGenderIs(String userName, String nickName, String gender,Integer pageNo, Integer pageSize) {
        Pageable pageable = PageRequest.of(pageNo, pageSize,Sort.by(Sort.Direction.DESC, "classId"));
        Specification<StudentInfo> spec = (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (userName != null && !userName.isEmpty()) {
                predicates.add(cb.like(root.get("userName"), "%" + userName + "%"));
            }
            if (gender != null && !gender.isEmpty()) {
                predicates.add(cb.equal(root.get("gender"), gender));
            }
            if (nickName != null && !nickName.isEmpty()) {
                predicates.add(cb.like(root.get("nickName"), "%" + nickName + "%"));
            }
            return cb.and(predicates.toArray(new Predicate[0]));
        };
        return studentInfoRepository.findAll(spec, pageable);
    }

    public void deleteStudentInfoByUserId(String userId) {
        studentInfoRepository.deleteStudentInfoByUserId(userId);
    }
}
